package net.runman.common.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 数据库配置
 * @author L.cm 
 * @email: 596392912@qq.com 
 * @site:http://www.dreamlu.net
 * @date 2015年4月18日下午7:09:58
 */
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {
	@Autowired(required = false)
	private DataSource dataSource;
	
	// Druid连接池
	// 连接池配置：https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8

	/**
	 * Druid数据源
	 * @return DataSource
	 */
	@Bean(initMethod = "init", destroyMethod = "close")
	@ConfigurationProperties(prefix="datasource.druid")
	public DataSource dataSource() {
		return new DruidDataSource();
	}

	@Bean
	public PlatformTransactionManager transactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}

}
